package com.evan.uyclub;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.evan.uyclub.Other.LoadDialog;
import com.evan.uyclub.Other.SetClass;
import com.evan.uyclub.Other.Utlis;
import com.evan.uyclub.R;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.royrodriguez.transitionbutton.TransitionButton;
import com.sdsmdg.tastytoast.TastyToast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;

public class ActivityLogin extends AppCompatActivity {
    private BottomSheetDialog dialog;
    private LoadDialog loadDialog;
    private String user;
    private String pass = "jfkjcyxf666";
    private String name;
    private TwinklingRefreshLayout smart;
    private LinearLayout all_main_reg;
    private MaterialEditText users, names;
    private CardView reg1;
    private TransitionButton Translate;
    private SharedPreferences data;
    private TextView dl_text, dl_text1;

    public void ActivityRegd() {
        loadDialog = new LoadDialog(this);
        loadDialog.show(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取TransitionButton id
        Translate = findViewById(R.id.Translate);
        //获取文本设置动画和字体
        dl_text = findViewById(R.id.dl_text);
        dl_text1 = findViewById(R.id.dl_text1);
        dl_text.setTypeface(Typeface.createFromAsset(getAssets(), "english.ttf"));
        dl_text1.setTypeface(Typeface.createFromAsset(getAssets(), "rubik.ttf"));
        //实现回弹效果
        smart = findViewById(R.id.smart);
        smart.setPureScrollModeOn();
        all_main_reg = findViewById(R.id.all_main_regs);
        //和逐渐显示动画
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(all_main_reg, "alpha", 0f, 1f);
        objectAnimator.setDuration(1500);
        objectAnimator.start();
        users = findViewById(R.id.user);
        reg1 = findViewById(R.id.login1);
        Reg();
    }

    private void Reg() {
        reg1.setOnClickListener(view -> {
            user = Objects.requireNonNull(users.getText()).toString();
            if (user.equals("")) {
                TastyToast.makeText(ActivityLogin.this, "输入信息哦~", TastyToast.LENGTH_SHORT, TastyToast.CONFUSING);
            } else {
                //弹出底部动画
                ActivityRegd();
                //延迟执行
                new Handler().postDelayed(this::Logins, 1500);
            }
        });
    }

    private void Logins() {
        TastyToast.makeText(ActivityLogin.this, "已关闭注册登录功能", TastyToast.LENGTH_SHORT, TastyToast.INFO);
        if (!Utlis.isVPNConnected(this)) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(SetClass.getUrl() + "uyapi/uyyun_login.php?" + "keeper=" + SetClass.getAdmin() + "&api=" + SetClass.getApi() + "&user=" + ActivityLogin.this.user + "&pass=" + pass);
                        URLConnection backtraclk = url.openConnection();
                        InputStreamReader back = new InputStreamReader(backtraclk.getInputStream());
                        BufferedReader Readerl = new BufferedReader(back);
                        String datas = Readerl.readLine();
                        //System.out.println(datas);
                        if (datas.contains("登录成功")) {
                            data = getSharedPreferences("data", MODE_PRIVATE);
                            SharedPreferences.Editor editor = data.edit();
                            editor.putString("user", user);
                            editor.commit();
                            loadDialog.dismiss();
                            Looper.prepare();
                            TastyToast.makeText(ActivityLogin.this, "登录成功咯~", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS);
                            Intent intent = new Intent();
                            intent.setClass(ActivityLogin.this, StartActivity.class);
                            startActivity(intent);
                            overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
                            Looper.loop();
                            finish();
                        } else {
                            loadDialog.dismiss();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    BottomSheetDialog dialog = new BottomSheetDialog(ActivityLogin.this);
                                    View dialog1 = getLayoutInflater().inflate(R.layout.gg_my, null);
                                    dialog.setContentView(dialog1);
                                    dialog.setCancelable(true);
                                    dialog.setCanceledOnTouchOutside(true);
                                    dialog.show();
                                    dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(R.color.bantouming);
                                    if (datas.contains("封禁")) {
                                        CardView lxzz = dialog1.findViewById(R.id.lxzz);
                                        lxzz.setVisibility(View.VISIBLE);
                                        lxzz.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + "3487297072" + "&version=1")));
                                            }
                                        });
                                    }
                                    TextView texts = dialog1.findViewById(R.id.App_text);
                                    texts.setText("系统提示");
                                    TextView sdl = dialog1.findViewById(R.id.sdl);
                                    sdl.setText("确认");
                                    sdl.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            dialog.dismiss();
                                        }
                                    });
                                    TextView fhyy = dialog1.findViewById(R.id.my_gg);
                                    //更新UI的操作代码
                                    //比如更新TextView显示的内容
                                    try {
                                        fhyy.setText(datas);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public void reg_end(View view) {
        //退出界面，直接退app
       /*finish();
       overridePendingTransition(R.anim.activity_visible,R.anim.activity_gone);*/
        TastyToast.makeText(ActivityLogin.this, "亲，好的在里面呢~", TastyToast.LENGTH_SHORT, TastyToast.INFO);
    }

}